package com.shq.ex1;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

import butterknife.ButterKnife;
import butterknife.BindView;

public class MulDingShiActivity extends AppCompatActivity implements View.OnClickListener {

    @BindView(R.id.mdsBtn)
    Button mdsBtn;
    @BindView(R.id.mdsTimeText)
    TextView mdsTimeText;
    private SharedPreferences sharedPreferences = null;
    private AlarmManager alarm = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mul_ding_shi);
        ButterKnife.bind(this);

        mdsBtn.setOnClickListener(this);

        sharedPreferences = getSharedPreferences("alarm_record",MODE_PRIVATE);
        alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
        Intent intent = new Intent(MulDingShiActivity.this,AlarmReceive.class);
        PendingIntent pi = PendingIntent.getBroadcast(MulDingShiActivity.this,0,
                intent,0);
        alarm.setRepeating(AlarmManager.RTC,0,60*1000,pi);
    }
    @Override
    public void onClick(View v) {
        Calendar calendarDialog = Calendar.getInstance();
        new TimePickerDialog(MulDingShiActivity.this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
                calendar.set(Calendar.MINUTE,minute);
                String timeStr = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY))
                        +":"+String.valueOf(calendar.get(Calendar.MINUTE));
                sharedPreferences.edit().putString(timeStr,timeStr).commit();
                //alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);//设置闹铃
                Toast.makeText(MulDingShiActivity.this,"闹铃设置成功",
                        Toast.LENGTH_LONG).show();
                mdsTimeText.setText(mdsTimeText.getText().toString() +
                        "\n"+"您设置的时间为："+calendar.get(Calendar.HOUR_OF_DAY) + "点"+
                        calendar.get(Calendar.MINUTE) + "分");
            }
        },calendarDialog.get(Calendar.HOUR_OF_DAY),calendarDialog.get(Calendar.MINUTE),
                true).show();
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
